package com.starsoft.quant.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.starsoft.frame.core.orm.PageObject;
import com.starsoft.frame.util.MapUtil;
import com.starsoft.frame.util.StringUtil;
import com.starsoft.quant.bean.QuantModel;
import com.starsoft.quant.bean.QuantModelColumn;
import com.starsoft.quant.dao.QuantModelDao;

@Service
public class QuantModelService {
	
	@Autowired
	QuantModelDao modelDao;
	
	public QuantModel get(String code){
		return modelDao.get(code);
	}
	
	public Map<String, Object> pageQuery(PageObject po){
		return modelDao.pageQuery(po);
	}
	
	public void save(QuantModel quantModel){
		modelDao.saveOrUpdate(quantModel);
	}
	
	public void delete(QuantModel quantModel){
		modelDao.delete(quantModel);
	}
	
	public List<QuantModelColumn> getOrderedColumns(QuantModel model) {
		Map<String, Object> mappedModel = MapUtil.toMap(model);
		List<QuantModelColumn> result = new ArrayList<QuantModelColumn>();
		String key = "col";
		for (int i = 1; i <= 20; i++) {
			if (!StringUtil.isEmpty(mappedModel.get(key + i))) {
				QuantModelColumn column = new QuantModelColumn();
				column.setFieldName(key + i);
				column.setName((String) mappedModel.get(key + i));
				column.setType((String) mappedModel.get(key + i + 't'));
				column.setFormat((String) mappedModel.get(key + i + 'f'));
				column.setOrder((Integer) mappedModel.get(key + i + 'o'));
				result.add(column);
			}
		}
		Collections.sort(result);
		return result;
	}
	
	public List<QuantModelColumn> getOrderedNumberColumns(QuantModel model) {
		Map<String, Object> mappedModel = MapUtil.toMap(model);
		List<QuantModelColumn> result = new ArrayList<QuantModelColumn>();
		String key = "col";
		for (int i = 1; i <= 20; i++) {
			if (!StringUtil.isEmpty(mappedModel.get(key + i))) {
				String coltype = (String) mappedModel.get(key + i + 't');
				if ("double".equalsIgnoreCase(coltype)) {
					QuantModelColumn column = new QuantModelColumn();
					column.setFieldName(key + i);
					column.setName((String) mappedModel.get(key + i));
					column.setType((String) mappedModel.get(key + i + 't'));
					column.setFormat((String) mappedModel.get(key + i + 'f'));
					column.setOrder((Integer) mappedModel.get(key + i + 'o'));
					result.add(column);
				}
			}
		}
		
		QuantModelColumn column = new QuantModelColumn();
		column = new QuantModelColumn();
		column.setFieldName("rating");
		column.setName("评级");
		column.setOrder(-10);
		result.add(column);
		
		column = new QuantModelColumn();
		column.setFieldName("price");
		column.setName("价格");
		column.setOrder(-8);
		result.add(column);
		
		column = new QuantModelColumn();
		column.setFieldName("priceChp");
		column.setName("涨幅");
		column.setOrder(-7);
		result.add(column);
		
		Collections.sort(result);
		return result;
	}
	
}
